<template>
  <basic-container>
    <avue-crud :option="option" :search.sync="search" :table-loading="loading" :data="data" :page.sync="page"
      :permission="permissionList" :before-open="beforeOpen" v-model="form" ref="crud" @row-update="rowUpdate"
      @row-save="rowSave" @row-del="rowDel" @search-change="searchChange" @search-reset="searchReset"
      @selection-change="selectionChange" @current-change="currentChange" @size-change="sizeChange"
      @refresh-change="refreshChange" @sort-change="sortChange" @on-load="onLoad">
      <template slot="menuRight">
        <el-button type="warning" size="small" plain icon="el-icon-download" v-if="permissionList.exportBtn" @click="handleExport" >导出
        </el-button>
      </template>

      <template slot-scope="{}" slot="serviceNoSearch">
        <select-page ref="searchSerSelect" label="serviceName" value="serviceNo" :select.sync="search.serviceNo" :url="website.searchLink.serviceUrl" placeholder-text="服务商" filter-key="searchParam"></select-page>
      </template>
      <template slot-scope="{}" slot="orgNoSearch">
        <select-page ref="searchOrgSelect" label="serviceName" value="serviceNo" :select.sync="search.orgNo" :url="website.searchLink.orgUrl" placeholder-text="机构" filter-key="searchParam"></select-page>
      </template>

      <!-- <template slot-scope="{}" slot="sum30Search">
        <select-page ref="searchsum30" label="rpiName" value="sum30" :select.sync="search.sum30" filter-key="searchParam" :url="website.searchLink.merchantUrl" placeholder-text="行业分类"></select-page>
      </template>
      <template slot-scope="{}" slot="sum31Search">
        <select-page ref="searchsum31" label="rpiName" value="sum31" :select.sync="search.sum31" filter-key="searchParam" :url="website.searchLink.merchantUrl" placeholder-text="行业大类"></select-page>
      </template>
      <template slot-scope="{}" slot="sum32Search">
        <select-page ref="searchsum32" label="rpiName" value="sum32" :select.sync="search.sum32" filter-key="searchParam" :url="website.searchLink.merchantUrl" placeholder-text="行业小类"></select-page>
      </template>
      <template slot-scope="{}" slot="sum33Search">
        <select-page ref="searchsum33" label="rpiName" value="sum33" :select.sync="search.sum33" filter-key="searchParam" :url="website.searchLink.merchantUrl" placeholder-text="品牌"></select-page>
      </template> -->
    </avue-crud>
  </basic-container>
</template>

<script>
import { apiList,apiExport} from "@/api/rb-oem/shop/deliveryList";
import { mapGetters } from "vuex";
import {downloadFileExcel} from "@/util/util";
export default {
  data() {
    return {
      form: {},
      query: {},
      search: {
        orderArr:[],
        deliverArr:[]
      },
      loading: false,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      option: {
        height: "calc(50vh - 50px)",
        calcHeight: 30,
        tip: false,
        border: true,
        index: true,
        viewBtn: false,
        editBtn: false,
        addBtn: false,
        delBtn: false,
        selection: false,
        searchMenuSpan: 24,
        columnBtn: false,
        labelWidth: 120, //弹出表单的标题的统一宽度
        searchLabelWidth: 120, //搜索框标题宽度
        menuWidth: 100, //操作栏宽度
        emptyBtnText:'重置',
        menu:false,
        column: [
          {
            label: "服务商编号",
            searchLabel: "服务商",
            prop: "serviceNo",
            type: 'select',
            search: true,
            overHidden: true,
            minWidth: 220,
          },
          {
            label: '',
            prop: 'isSelectChild',
            type: 'select',
            multiple: false,
            dataType: "string",
            showColumn: false,
            hide: true,
            search: true,
            searchType:"checkbox",
            searchLabelWidth: '0',
            dicData: [{
              label:'包含下级',
              value: 1
            }]
          },
          {
            label: "服务商名称",
            prop: "serviceName",
            type: 'input',
            overHidden: true,
            minWidth: 220,
          },
          {
            label: "机构编号",
            searchLabel: "机构",
            prop: "orgNo",
            overHidden: true,
            type: 'select',
            search: true,
            searchLabel: "机构",
            minWidth: 220,
          },
          {
            label: "机构名称",
            prop: "orgName",
            type: 'input',
            overHidden: true,
            minWidth: 220,
          },
          {
            label: "订单编号",
            prop: "orderCode",
            type: 'input',
            search:true,
            overHidden: true,
            minWidth: 220,
          },
          {
            label: "子订单编号",
            prop: "subOrderCode",
            type: 'input',
            search:true,
            overHidden: true,
            minWidth: 220,
          },
          {
            label: "行业分类",
            prop: "classIndustryName",
            type: 'input',
            overHidden: true,
          },
          {
            label: "行业大类",
            prop: "classSuperName",
            type: 'input',
            overHidden: true,
          },
          {
            label: "行业小类",
            prop: "classSubName",
            type: 'input',
            overHidden: true,
          },
          {
            label: "品牌",
            prop: "classBrandName",
            type: 'input',
            overHidden: true,
          },
          
          {
            label: "行业分类",
            prop: "classIndustry",
            type: "select",
            span: 24,
            search: true,
            props: {
              label: 'firstName',
              value: 'firstCode',
            },
            cascaderItem: ['classSuper','classSub','classBrand'],
            dicUrl:'/api/rb-oem/oem/mall/mall_class/list?current=-1&size=-1&type=1',
            dicFormatter:(data)=>{
              return data.data.records
            },
            rules: [{
              required: true,
              message: "请选择行业分类",
              trigger: "blur"
            }],
            hide: true,
          },
          {
            label: "行业大类",
            prop: "classSuper",
            type: "select",
            span: 24,
            search: true,
            props: {
              label: 'secondName',
              value: 'secondCode',
            },
            // cascader: ['classSub'],
            dicUrl:`/api/rb-oem/oem/mall/mall_class/list?current=-1&size=-1&type=2&firstCode={{key}}`,
            dicFormatter:(data)=>{
              return data.data.records
            },
            rules: [{
              required: true,
              message: "请选择行业大类",
              trigger: "blur"
            }],
            hide: true,
          },
          {
            label: "行业小类",
            prop: "classSub",
            type: "select",
            span: 24,
            search: true,
            props: {
              label: 'thirdName',
              value: 'thirdCode',
            },
            // cascader: ['classBrand'],
            dicUrl:`/api/rb-oem/oem/mall/mall_class/list?current=-1&size=-1&type=3&secondCode={{key}}`,
            dicFormatter:(data)=>{
              return data.data.records
            },
            rules: [{
              required: true,
              message: "请选择行业小类",
              trigger: "blur"
            }],
            hide: true,
          },
          {
            label: "品牌",
            prop: "classBrand",
            type: "select",
            span: 24,
            search: true,
            props: {
              label: 'fourthName',
              value: 'fourthCode',
            },
            dicUrl:`/api/rb-oem/oem/mall/mall_class/list?current=-1&size=-1&type=4&thirdCode={{key}}`,
            dicFormatter:(data)=>{
              return data.data.records
            },
            rules: [{
              required: true,
              message: "请选择行业小类",
              trigger: "blur"
            }],
            hide: true
          },
          {
            label: "型号规格",
            prop: "specificationName",
            type: 'input',
            overHidden: true,
          },
          {
            label: "设备编号",
            prop: "sn",
            type: 'input',
            overHidden: true,
          },
          {
            label: "订单日期",
            prop: "createTime",
            type: 'input',
            overHidden: true,
            minWidth: 140,
          },
          {
            label: "价格",
            prop: "price",
            type: 'input',
            overHidden: true,
          },
          {
            label: "数量",
            prop: "num",
            type: 'input',
            overHidden: true,
          },
          {
            label: "兑换券",
            prop: "couponCode",
            type: 'input',
            overHidden: true,
          },
          {
            label: "实付金额",
            prop: "realPayAmt",
            type: 'input',
            overHidden: true,
          },
          {
            label: "发货日期",
            prop: "deliverTime",
            type: 'input',
            overHidden: true,
            minWidth: 140,
          },
          {
            label: "订单日期",
            prop: "orderArr",
            type: "daterange",
            searchRange: true,
            format:'yyyy-MM-dd',
            valueFormat:'yyyy-MM-dd',
            startPlaceholder: "开始日期",
            endPlaceholder: "结束日期",
            search: true,
            hide: true,
            display: false,
            rangeSeparator:"至"
          },
          {
            label: "发货日期",
            prop: "deliverArr",
            type: "daterange",
            searchRange: true,
            format:'yyyy-MM-dd',
            valueFormat:'yyyy-MM-dd',
            startPlaceholder: "开始日期",
            endPlaceholder: "结束日期",
            search: true,
            hide: true,
            display: false,
            rangeSeparator:"至"
          },
        ]
      },
      data: []
    };
  },
  computed: {
    ...mapGetters(["permission"]),
    permissionList() {
      return {
        exportBtn: this.vaildData(this.permission.deliveryList_export, false),
        viewBtn: this.vaildData(this.permission.deliveryList_view, false),
      };
    },
  },
  methods: {
    /**
     * @func 导出
     * @desc 根据条件导出
     * */
     handleExport() {
      let params ={
        ...this.search,
        current:'-1',
        size:'-1'
      }
      if (this.search.orderArr.length>0) {
        params.orderBeginTime = this.search.orderArr[0]
        params.orderEndTime = this.search.orderArr[1]
      }

      if (this.search.deliverArr.length>0) {
        params.deliverBeginTime = this.search.deliverArr[0]
        params.deliverEndTime = this.search.deliverArr[1]
      }

      this.$confirm("是否导出用户数据?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
       apiExport(params).then((res) => {
            downloadFileExcel(res);
          }
        );
      });
    },
    beforeOpen(done, type) {
      if (["edit", "view"].includes(type)) {
        getDetail(this.form.id).then(res => {
          this.form = res.data.data;
        });
      }
      done();
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;

      if (this.search.orderArr.length>0) {
        params.orderBeginTime = this.search.orderArr[0]
        params.orderEndTime = this.search.orderArr[1]
      }

      if (this.search.deliverArr.length>0) {
        params.deliverBeginTime = this.search.deliverArr[0]
        params.deliverEndTime = this.search.deliverArr[1]
      }

      apiList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    },
    sortChange(val) {
      if (val.order == "ascending") {
        //正序
        this.query.ascs = val.prop
        this.query.descs = ""
      } else {
        //倒序
        this.query.descs = val.prop
        this.query.ascs = ""
      }
      this.refreshChange();
    }
  }
};
</script>

<style scoped>
::v-deep .el-date-editor .el-range__close-icon{
  margin-right: 10px;
}
</style>
